using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Helpers;
	using System.Web.Security;
    using TemplateWeb.DTO;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectToChucCaNhan()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_TOCHUCCANHAN
			};
			return View(CreateViewModel(data));
		}



		public ActionResult DeleteToChucCaNhan(int id, int page = 1)
		{
			var result = _repToChucCaNhan.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_TOCHUCCANHAN,
				CurrentPage = page
			};
			return View("SelectToChucCaNhan", CreateViewModel(data));
		}

		public ActionResult EditToChucCaNhan(int page, int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_TOCHUCCANHAN,
				CurrentPage = page,
				IntID = id
			};
			return PartialView("Templates/TH_EditToChucCaNhan", CreateViewModel(data));
		}

		public ActionResult CancelEditingToChucCaNhan(int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_TOCHUCCANHAN,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListToChucCaNhan", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateToChucCaNhan(FormCollection forms)
		{
			string id = forms.GetValues("ToChucCaNhan_ID")[0];
			string tentochuccanhan = forms.GetValues("ToChucCaNhan_TenToChucCaNhan")[0];
			string diachi = forms.GetValues("ToChucCaNhan_DiaChi")[0];
			string sodienthoai = forms.GetValues("ToChucCaNhan_SoDienThoai")[0];
			var tochuccanhan = _repToChucCaNhan.SelectByID(int.Parse(id));
			tochuccanhan.TenToChucCaNhan = tentochuccanhan;
			tochuccanhan.DiaChi = diachi;
			tochuccanhan.SoDienThoai = sodienthoai;
			var result = _repToChucCaNhan.Update(tochuccanhan);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_TOCHUCCANHAN,
			};
			return View("SelectToChucCaNhan", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertToChucCaNhan(FormCollection forms)
		{
			string tentochuccanhan = forms.GetValues("ToChucCaNhan_TenToChucCaNhan")[0];
			string diachi = forms.GetValues("ToChucCaNhan_DiaChi")[0];
			string sodienthoai = forms.GetValues("ToChucCaNhan_SoDienThoai")[0];
			var newItem = new ToChucCaNhanDTO
			{
				TenToChucCaNhan = tentochuccanhan,
				DiaChi = diachi,
				SoDienThoai = sodienthoai
			};
			var result = _repToChucCaNhan.Insert(newItem);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_TOCHUCCANHAN,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectToChucCaNhan", CreateViewModel(data));
		}


	}
}